package com.example.idphotogenerator.service;

import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

@Service
public class PhotoService {

    @Value("${hivision.api.id_photo_url}")
    private String idPhotoApiUrl;

    @Value("${hivision.api.add_background_url}")
    private String addBackgroundApiUrl;

    private final OkHttpClient httpClient = new OkHttpClient();

    public String generateIdPhoto(MultipartFile inputImage, int width, int height) {
        try {
            // Create a request body with the file and additional parameters
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("input_image", inputImage.getOriginalFilename(),
                            RequestBody.create(inputImage.getBytes(), MediaType.parse("image/jpeg"))) // Adjust media type as necessary
                    .addFormDataPart("width", String.valueOf(width)) // Add width parameter
                    .addFormDataPart("height", String.valueOf(height)) // Add height parameter
                    .build();

            // Create the request
            Request request = new Request.Builder()
                    .url(idPhotoApiUrl)
                    .post(requestBody)
                    .build();

            // Execute the request
            try (Response response = httpClient.newCall(request).execute()) {
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                return response.body().string(); // Return the response from the API
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error processing ID photo: " + e.getMessage();
        }
    }

    public String addBackground(String imageBase64, String color) {
        try {
            // Check if the base64 string starts with the prefix and strip it
            if (imageBase64.startsWith("data:image/png;base64,")) {
                imageBase64 = imageBase64.replace("data:image/png;base64,", "");
            }

            // Decode the base64 string to bytes
            byte[] imageBytes = Base64.getDecoder().decode(imageBase64);

            // Create a request body with the file
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("input_image", "image.png", 
                            RequestBody.create(imageBytes, MediaType.parse("image/png"))) // Adjust media type as necessary
                    .addFormDataPart("color", color)
                    .build();

            // Create the request
            Request request = new Request.Builder()
                    .url(addBackgroundApiUrl)
                    .post(requestBody)
                    .build();

            // Execute the request
            try (Response response = httpClient.newCall(request).execute()) {
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                return response.body().string(); // Return the response from the API
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Error adding background: " + e.getMessage();
        }
    }
} 